package osb.map.main;

import java.util.ArrayList;
import java.util.HashMap;
import osb.map.database.ConfigurationRW;
import osb.map.map.Map;
import osb.map.net.ClientSocket;
import osb.map.net.DataPackage;
import osb.map.net.DataPay;
import osb.map.net.msgType;
import osb.map.publicdata.Public_Operate_And_Data;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Gravity;
import android.view.InflateException;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.ljp.ani.R;

/**
 * 主界面
 * @author Administrator
 *
 */

public class Main_Activity extends Activity
{
	public static Main_Activity Public_Static_OSBActivity; 
	static boolean isopen = false;
	private PopupWindow popupWindow;
	private Button buttontest;
	private Main_View joke;  
	
	private boolean IsOpenMenu = true;
	
	// 支付listview
	ListView list;
	
	// 支付listview内容
	String data[] = new String[]
	{ "支付小绿编号001", "支付小绿编号002", "支付小绿编号003", "支付小绿编号004", "支付小绿编号005", "支付小绿编号006" };
	
	// 支付listview的图片
	int[] menu_image_array =
	{ R.drawable.ccc, R.drawable.cccc, R.drawable.cccc, R.drawable.ccc, R.drawable.cccc, R.drawable.ccc, };

	//ui风格listview
	ListView list_ui;
	
	// ui风格listview的内容
	String data_ui[] = new String[]
	{ "绿(默认)", "涂鸦" , "油画"};
	 
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(Public_Operate_And_Data.UI_Stype_Curren);
		Public_Static_OSBActivity = this;
		
		// 判断网络是否连接
		if ((!Public_Operate_And_Data.IsConnectSuccess)&&(!Public_Operate_And_Data.IsChangeUIStype))
		{
			Toast.makeText(this, "连接服务器失败，您可能无法获得部分服务", Toast.LENGTH_SHORT).show();
		}
		 //开始下载评论消息
		if (Public_Operate_And_Data.IsConnectSuccess)   
		{
			DataPackage data=new DataPackage(msgType.Comment_Server_Reflash_First);
			ClientSocket.Public_Static_ClientSocket.sendMsg(data);
		}
		
		// ui风格初始化
		if(Public_Operate_And_Data.IsChangeUIStype)   
		{
			Public_Operate_And_Data.Public_Static_OSBActivity_UIchange.finish();  
			Public_Operate_And_Data.IsChangeUIStype=false; 
		}

		// 登陆按钮
		joke = (Main_View) findViewById(R.id.c_login); // 登陆
		joke.setOnClickIntent(new Main_View.OnViewClick()
		{
			@Override
			public void onClick()
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Login_Main.class); 
				startActivity(intent);
			}
			@Override
			public void onClick1()
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Login_Main.class); 
				startActivity(intent);
			}
		});
		// 付款按钮
		joke = (Main_View) findViewById(R.id.c_pay); // 付款
		joke.setOnClickIntent(new Main_View.OnViewClick()
		{
			@Override
			public void onClick()
			{
				// 判断用户是否登录
				if (Public_Operate_And_Data.Islogin)
				{
					openPopupwin(); 
				} else
				{
					new AlertDialog.Builder(Main_Activity.this).setMessage("您需要登录才能执行此操作").setPositiveButton("确定", null).show();
				}
			}
			@Override
			public void onClick1()
			{
				if (Public_Operate_And_Data.Islogin)
				{
					openPopupwin(); 
				} else
				{
					new AlertDialog.Builder(Main_Activity.this).setMessage("您需要登录才能执行此操作").setPositiveButton("确定", null).show();
				}

			}
		});
		// 评论 - 画墙 按钮
		joke = (Main_View) findViewById(R.id.c_comment); 
		joke.setOnClickIntent(new Main_View.OnViewClick()
		{
			// 评论按钮
			@Override
			public void onClick()
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Comment.class);
				startActivity(intent);
			}
			// 画墙按钮
			@Override
			public void onClick1()  
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, HuaQiang_Main.class); 
				startActivity(intent);
			}
		});
		// 地图按钮 
		joke = (Main_View) findViewById(R.id.c_access);  
		joke.setOnClickIntent(new Main_View.OnViewClick()
		{
			@Override
			public void onClick()
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Map.class); 
				startActivity(intent);
			}
			@Override
			public void onClick1()
			{
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Map.class);
				startActivity(intent);
			}
		});


		Button btn_about = (Button) findViewById(R.id.button_about);
		Button btn_choose = (Button) findViewById(R.id.button_choose);
		btn_about.setOnClickListener(new OnClickListener()
		{
			// 关于按钮 - 进入欢迎引导界面
			@Override
			public void onClick(View v)    
			{
				Public_Operate_And_Data.Showguide=true;
				Intent intent = new Intent();
				intent.setClass(Main_Activity.this, Welcome_Firsttime_In_main.class); 
				startActivity(intent);
				Main_Activity.this.finish();
			}
		});
		btn_choose.setOnClickListener(new OnClickListener()
		{
			// 风格选择按钮 
			@Override
			public void onClick(View v)  
			{ 
				openPopupwin_uistype();
			}
		}); 
	}

	/**
	 * 付款popup
	 */
	private static String busID;
	private void openPopupwin()
	{
		LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(R.layout.pay_popupwindow, null, true);
		list = (ListView) menuView.findViewById(R.id.test_listView1);
		SimpleAdapter adapter = new SimpleAdapter(this, getData(),
				R.layout.pay_list_item, new String[]
				{ "title", "img" }, new int[]
				{ R.id.title, R.drawable.usertest });
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
			{
				switch (arg2)
				{
				case 0:
					busID = "1";
					break;
				case 1:
					busID = "2";
					break;
				case 2:
					busID = "3";
					break;
				case 3:
					busID = "4";
					break;
				case 4:
					busID = "5";
					break;
				case 5:
					busID = "6";
					break;
				}
				// 选择了小绿的编号后 跳转到确认
				new AlertDialog.Builder(Main_Activity.Public_Static_OSBActivity)
						.setTitle("支付确认")
						.setMessage("您将要支付编号:小绿" + busID + "  金额:1元")
						.setPositiveButton("确认",
								new DialogInterface.OnClickListener()
								{
							        // 确认支付
									public void onClick( DialogInterface dialoginterface, int i)
									{
										if(Public_Operate_And_Data.User_accout<=0)   //用户余额不足
										{
											new AlertDialog.Builder(Main_Activity.Public_Static_OSBActivity)
											.setMessage("您的余额不足，无法付费")
											.setPositiveButton("确定", null)
											.show();
											return;
										}
										String userid = Public_Operate_And_Data.User_id;
										String xiaolvid = busID;
										DataPackage data = new DataPackage( msgType.Pay_User);
										data.dataPay = new DataPay(userid, xiaolvid);
										ClientSocket.Public_Static_ClientSocket .sendMsg(data);
										Toast.makeText(Main_Activity.Public_Static_OSBActivity, "付款中。请稍候", Toast.LENGTH_SHORT).show();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener()
								{
									public void onClick( DialogInterface dialoginterface, int i)
									{ 
									}
								}).show();
			}
		});

		buttontest = (Button) menuView.findViewById(R.id.XIAOLV_back);
		buttontest.requestFocus();
		buttontest.setOnClickListener(new OnClickListener()
		{
			// 关闭支付对话框
			@Override
			public void onClick(View v)
			{
				popupWindow.dismiss();
			}
		});
		popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.showAtLocation(findViewById(R.id.pop_parents),Gravity.CENTER | Gravity.CENTER, 0, 0); 
		popupWindow.update();
	}

	/**
	 * 支付适配器 - 数据构造函数
	 * @return
	 */
	private ArrayList<HashMap<String, Object>> getData()
	{
		ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String, Object>>();

		for (int i = 0; i < data.length; i++)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("title", data[i]);
			map.put("img", menu_image_array[i]);
			dlist.add(map);
		}
		return dlist;
	}
	
	/**
	 * ui选择 popup
	 */
	private void openPopupwin_uistype()
	{
	    LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
	    ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate( R.layout.ui_popupwindow, null, true);
	    list_ui = (ListView) menuView.findViewById(R.id.ui_test_listView1);
        SimpleAdapter adapter = new SimpleAdapter(this, getData_ui(),R.layout.ui_list_item, new String[]
	    { "ui_title", "ui_img" }, new int[]
	    { R.id.ui_title, R.drawable.usertest });
	    list_ui.setAdapter(adapter);
	    // 设置列表响应事件
	    list_ui.setOnItemClickListener(new OnItemClickListener()
	    {
	        @Override
	        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
	        {
	            switch (arg2)
	            {
	            case 0:   //绿(默认)
	                    ChangeUIStype(Public_Operate_And_Data.UI_Stype_green, 0);
	                break;
	            case 1:   //涂鸦
	                ChangeUIStype(Public_Operate_And_Data.UI_Stype_tuya, 1);
	                break;
//	            case 2:   //萝莉
//	                ChangeUIStype(Public_Operate_And_Data.UI_Stype_loli, 2);
//	                break;
	            case 2:   //抽象
	                ChangeUIStype(Public_Operate_And_Data.UI_Stype_chouxiang, 2);
	                break;
	            }

	        }
	    });
	    buttontest = (Button) menuView.findViewById(R.id.ui_back);
	    buttontest.requestFocus();
	    buttontest.setOnClickListener(new OnClickListener()
	    {
	    	// 关闭ui风格对话框
	        @Override
	        public void onClick(View v)
	        {
	            popupWindow.dismiss(); 
	        }
	    });
	    popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
	    LayoutParams.FILL_PARENT, true);
	    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
	    popupWindow.showAtLocation(findViewById(R.id.pop_parents), Gravity.CENTER | Gravity.CENTER, 0, 0); 
	    popupWindow.update();
	}

	/**
	 * ui风格对话框适配器 - 数据构造函数
	 * @return
	 */
	private ArrayList<HashMap<String, Object>> getData_ui()
	{
	    ArrayList<HashMap<String, Object>> dlist = new ArrayList<HashMap<String, Object>>();

	    for (int i = 0; i < data_ui.length; i++)
	    {
	        HashMap<String, Object> map = new HashMap<String, Object>();
	        map.put("ui_title", data_ui[i]);
	        map.put("ui_img", menu_image_array[i]);
	        dlist.add(map);
	    }
	    return dlist;
	}
	
	/**
	 * 支付成功
	 */
	public void paysuccess()
	{
		// 余额减1
	    Public_Operate_And_Data.User_accout--;   
	    ConfigurationRW.Configuration_W_this(null, null, null, null, "" + Public_Operate_And_Data.User_accout, this);
	    new AlertDialog.Builder(Main_Activity.this)
	    .setMessage("支付成功")
	    .setPositiveButton("确定", null)
	    .show();
	}

	/**
	 * 支付失败
	 */
	public void payfalse()  
	{
	    new AlertDialog.Builder(Main_Activity.this)
	    .setMessage("支付失败")
	    .setPositiveButton("确定", null)
	    .show();
	}

	/**
	 * 修改UI风格
	 * @param stype   类型
	 * @param sign    标志位
	 */
	private void ChangeUIStype(int stype, int sign) 
	{
		Public_Operate_And_Data.UI_Stype_INT_Curren =sign;
	    Public_Operate_And_Data.UI_Stype_Curren = stype;
	    popupWindow.dismiss(); 
	    Intent intent = new Intent();
	    intent.setClass(Main_Activity.this, Main_Activity.class); 
	    startActivity(intent);
	    Public_Operate_And_Data.Public_Static_OSBActivity_UIchange = Main_Activity.this;
	    Public_Operate_And_Data.IsChangeUIStype = true;
	    ConfigurationRW.Configuration_W_this(null, null, "" + sign, null, null, this); 
	}
	


	
	/**
	 * menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 3, "分享下载").setIcon(android.R.drawable.ic_menu_send);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent intent = new Intent();
			intent.setClass(Main_Activity.this, ShareActivity.class);
			Main_Activity.this.startActivity(intent);

			break;
		}
		return false;
	}

	private long exitTime = 0;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK)
	    {
	        if ((System.currentTimeMillis() - exitTime) > 2000)
	        {
	            Toast.makeText(getApplicationContext(), "再按一次退出程序",
	            Toast.LENGTH_SHORT).show();
	            exitTime = System.currentTimeMillis();
	        }
	        else
	        {
	            finish();
	            System.exit(0);
	        }
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	}